package com.controller;

import com.entity.*;
import com.service.AddintoService;
import com.service.BranchService;
import com.service.StorageDetailsService;
import com.service.StorageHistoryService;
import com.service.impl.BranchServiceImpl;
import com.service.impl.StorageDetailsServiceImpl;
import com.service.impl.StorageHistoryServiceImpl;
import com.until.Result;
import com.vo.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author :hcy
 * @date:
 * @description:
 */
@RestController
@RequestMapping("addinto")
public class AddintoController extends BaseController {
    @Resource
    private AddintoService addintoService;
    @Resource
    private BranchService branchService;
    @Resource
    private StorageHistoryService storageHistoryService;
    @Resource
    private StorageDetailsService storageDetailsService;

    @PostMapping("listAllAddinto11")
    public Result listAllAddintoResult(@RequestBody Page<Addinto> page) {
        return success(addintoService.listAllAddinto(page));
    }

    @GetMapping("getByIdAddinto1")
    public Result getByIdAddinto(Integer addintoId) {
        return success(addintoService.getByIdAddinto(addintoId));
    }

    @PostMapping("addAddinto")
    public Result addAddinto(@RequestBody Addinto addinto) {
        return success(addintoService.addAddinto(addinto));
    }

    @PostMapping("updateAddinto")
    public Result updateAddinto(@RequestBody Addinto addinto) {
        return success(addintoService.updateAddinto(addinto));
    }

    @DeleteMapping("deleteAddinto")
    public Result deleteAddinto(Integer addintoId) {
        return success(addintoService.deleteAddinto(addintoId));
    }

    @RequestMapping("addCounts")
    public Result addCounts(String multipleSelection) {
        System.out.println(multipleSelection);
        return null;
    }

    @PostMapping("listByParam")
    public Result listByParam(@RequestBody Page<Branch> page, HttpSession session) {
        page.getData().setStoresId(getSession(session).getStoresId());
        return success(branchService.listByParam(page));
    }

    @PostMapping("insertOne")
    public Result insertOne(@RequestBody Branch branch, HttpSession session) {
        double amount = branch.getAddStock() *
                branch.getBook().getInPrice();
        StorageHistory storageHistory = new StorageHistory();
        StorageDetails storageDetails = new StorageDetails();
        storageHistory.setShAmount(amount);
        storageHistory.setShSum(branch.getAddStock());
        Emp emp = getSession(session);

        storageHistory.setShMan(emp.getEmpName());
        storageHistoryService.insertOne(storageHistory);

        branch.setStoresId(emp.getStoresId());

        branch.setBookQty(branch.getBookQty() + branch.getAddStock());
        branchService.update(branch);


        storageDetails.setShId(storageHistory.getShId());
        storageDetails.setBookId(branch.getBookId());
        storageDetails.setStorageQty(branch.getAddStock());
        storageDetails.setStorageMoneny(amount);
        return success(storageDetailsService.insert(storageDetails));
    }

    @PostMapping("updateMore")
    public Result insertMore(@RequestBody List<Branch> branchList, HttpSession session) {
        StorageHistory storageHistory = new StorageHistory();
        ArrayList<StorageDetails> StorageDetailsList = new ArrayList<>();
        Emp emp = getSession(session);
        //入库总金额
        double amount = 0;
        //入库总数
        int sum = 0;
        storageHistory.setShMan(emp.getEmpName());
        for (Branch branch : branchList) {
            //循环计算总数，总金额
            amount += branch.getAddStock() * branch.getBook().getInPrice();
            sum += branch.getAddStock();
            branch.setStoresId(emp.getStoresId());
        }
        storageHistory.setShSum(sum);
        storageHistory.setShAmount(amount);
        //入库历史
        storageHistoryService.insertMore(storageHistory);
        //循环每条订单详情
        for (Branch branch : branchList) {
            StorageDetails storageDetails = new StorageDetails();
            storageDetails.setShId(storageHistory.getShId());
            storageDetails.setBookId(branch.getBookId());
            storageDetails.setStorageQty(branch.getAddStock());
            storageDetails.setStorageMoneny(branch.getAddStock() * branch.getBook().getInPrice());
            StorageDetailsList.add(storageDetails);
        }

        //修改库存
        branchService.updateMore(branchList);

        //添加入库的每一条数据
        return success(storageDetailsService.insertMore(StorageDetailsList));
    }

}
